home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 317 / asmsrc / flonum-m.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  5.0 KB  |  196 lines

  1. /* flonum_multip.c - */
  2.  
  3. /* Copyright (C) 1987 Free Software Foundation, Inc.
  4.  
  5. This file is part of Gas, the GNU Assembler.
  6.  
  7. The GNU assembler is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY.  No author or distributor
  9. accepts responsibility to anyone for the consequences of using it
  10. or for whether it serves any particular purpose or works at all,
  11. unless he says so in writing.  Refer to the GNU Assembler General
  12. Public License for full details.
  13.  
  14. Everyone is granted permission to copy, modify and redistribute
  15. the GNU Assembler, but only under the conditions described in the
  16. GNU Assembler General Public License.  A copy of this license is
  17. supposed to have been given to you along with the GNU Assembler
  18. so you can know your rights and responsibilities.  It should be
  19. in a file named COPYING.  Among other things, the copyright
  20. notice and this notice must be preserved on all copies.  */
  21.  
  22. #include "flonum.h"
  23.  
  24. /*    plan for a . b => p(roduct)
  25.  
  26.  
  27.     +-------+-------+-/   /-+-------+-------+
  28.     | a    | a    |  ...    | a    | a    |
  29.     |  A    |  A-1    |    |  1    |  0    |
  30.     +-------+-------+-/   /-+-------+-------+
  31.  
  32.  
  33.     +-------+-------+-/   /-+-------+-------+
  34.     | b    | b    |  ...    | b    | b    |
  35.     |  B    |  B-1    |    |  1    |  0    |
  36.     +-------+-------+-/   /-+-------+-------+
  37.  
  38.  
  39.     +-------+-------+-/   /-+-------+-/   /-+-------+-------+
  40.     | p    | p    |  ...    | p    |  ...    | p    | p    |
  41.     |  A+B+1|  A+B    |    |  N    |    |  1    |  0    |
  42.     +-------+-------+-/   /-+-------+-/   /-+-------+-------+
  43.  
  44.                    /^\
  45.      (carry) a .b       ...        |       ...     a .b     a .b
  46.           A  B             |          0  1      0  0
  47.                     |
  48.                ...        |       ...     a .b
  49.                     |          1  0
  50.                     |
  51.                     |       ...
  52.                     |
  53.                     |
  54.                     |
  55.                     |          ___
  56.                     |          \
  57.                     +-----  P  =   >  a .b
  58.                          N      /__  i  j
  59.  
  60.                     N = 0 ... A+B
  61.  
  62.                     for all i,j where i+j=N
  63.                     [i,j integers > 0]
  64.  
  65. a[], b[], p[] may not intersect.
  66. Zero length factors signify 0 significant bits: treat as 0.0.
  67. 0.0 factors do the right thing.
  68. Zero length product OK.
  69.  
  70. I chose the ForTran accent "foo[bar]" instead of the C accent "*garply"
  71. because I felt the ForTran way was more intuitive. The C way would
  72. probably yield better code on most C compilers. Dean Elsner.
  73. (C style also gives deeper insight [to me] ... oh well ...)
  74. */
  75.  
  76. void
  77. flonum_multip (a, b, product)
  78.      FLONUM_TYPE *    a,
  79.          *    b,
  80.          *    product;
  81. {
  82.   int            size_of_a;        /* 0 origin */
  83.   int            size_of_b;        /* 0 origin */
  84.   int            size_of_product;    /* 0 origin */
  85.   int            size_of_sum;        /* 0 origin */
  86.   int            extra_product_positions;/* 1 origin */
  87.   unsigned long int    work;
  88.   unsigned long int    carry;
  89.   long int        exponent;
  90.   LITTLENUM_TYPE *    q;
  91.   long int        significant;        /* TRUE when we emit a non-0 littlenum  */
  92.                 /* ForTran accent follows. */
  93.   int            P;    /* Scan product low-order -> high. */
  94.   int            N;    /* As in sum above.  */
  95.   int            A;    /* Which [] of a? */
  96.   int            B;    /* Which [] of b? */
  97.  
  98.   product -> sign = (a->sign == b->sign) ? '+' : '-';
  99.   size_of_a        = a       -> leader    -  a       -> low;
  100.   size_of_b        = b       -> leader    -  b       -> low;
  101.   exponent        = a      -> exponent    +  b       -> exponent;
  102.   size_of_product    = product -> high    -  product -> low;
  103.   size_of_sum        = size_of_a        +  size_of_b;
  104.   extra_product_positions  =  size_of_product  -  size_of_sum;
  105.   if (extra_product_positions < 0)
  106.     {
  107.       P = extra_product_positions; /* P < 0 */
  108.       exponent -= extra_product_positions; /* Increases exponent. */
  109.     }
  110.   else
  111.     {
  112.       P = 0;
  113.     }
  114.   carry = 0;
  115.   significant = 0;
  116.   for (N = 0;
  117.        N <= size_of_sum;
  118.        N++)
  119.     {
  120.       work = carry;
  121.       carry = 0;
  122.       for (A = 0;
  123.        A <= N;
  124.        A ++)
  125.     {
  126.       B = N - A;
  127.       if (A <= size_of_a   &&   B <= size_of_b  &&  B >= 0)
  128.         {
  129. #ifdef TRACE
  130. printf("a:low[%d.]=%04x b:low[%d.]=%04x work_before=%08x\n", A, a->low[A], B, b->low[B], work);
  131. #endif
  132.           work += a -> low [A]   *   b -> low [B];
  133.           carry += work >> LITTLENUM_NUMBER_OF_BITS;
  134.           work &= LITTLENUM_MASK;
  135. #ifdef TRACE
  136. printf("work=%08x carry=%04x\n", work, carry);
  137. #endif
  138.         }
  139.     }
  140.       significant |= work;
  141.       if (significant)
  142.     {
  143.       if (P >= 0)
  144.         {
  145.           product -> low [P] = work;
  146. #ifdef TRACE
  147. printf("P=%d. work[p]:=%04x\n", P, work);
  148. #endif
  149.         }
  150.       P ++;
  151.     }
  152.       else
  153.     {
  154.       extra_product_positions ++;
  155.       exponent ++;
  156.     }
  157.     }
  158.   /*
  159.    * [P]-> position # size_of_sum + 1.
  160.    * This is where 'carry' should go.
  161.    */
  162. #ifdef TRACE
  163. printf("final carry =%04x\n", carry);
  164. #endif
  165.   if (carry)
  166.     {
  167.       if (extra_product_positions > 0)
  168.     {
  169.       product -> low [P] = carry;
  170.     }
  171.       else
  172.     {
  173.       /* No room at high order for carry littlenum. */
  174.       /* Shift right 1 to make room for most significant littlenum. */
  175.       exponent ++;
  176.       P --;
  177.       for (q  = product -> low + P;
  178.            q >= product -> low;
  179.            q --)
  180.         {
  181.           work = * q;
  182.           * q = carry;
  183.           carry = work;
  184.         }
  185.     }
  186.     }
  187.   else
  188.     {
  189.       P --;
  190.     }
  191.   product -> leader    = product -> low + P;
  192.   product -> exponent    = exponent;
  193. }
  194.  
  195. /* end: flonum_multip.c */
  196.